package com.rykj.qxj.server.common.utils;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.rykj.qxj.server.util.object.ObjectUtils;

public class StringUtils extends ObjectUtils {
	/**
	 * 数据已存在
	 */
	public static final String DATA_EXIST = "exist";
	/**
	 * 数据不存在
	 */
	public static final String DATA_UNEXIST = "unexist";
	
	/**
	 * unicode字符串判断
	 */
	static final Pattern REUNICODE = Pattern.compile("\\\\u([0-9a-zA-Z]{4})");
	/**
	 * 判断是否为中文字符
	 *<br/>Author <b>lilong@tsinghuabigdata.com</b><br/>
	 * 2014年6月12日 下午4:37:59<br/>
	 * @param c
	 * @return
	 */
	private static final boolean isChinese(char c) {
		// GENERAL_PUNCTUATION 判断中文的“号
		// CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号
		// HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的，号
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
			return true;
		}
		return false;
	}
	/**
	 * 判断是否为中文字符
	 *<br/>Author <b>lilong@tsinghuabigdata.com</b><br/>
	 * 2014年6月12日 下午4:37:59<br/>
	 * @param strName 需判断的数据
	 * @return true:中文字符 false:英文字符
	 */
	public static final boolean isChinese(String strName) {
		char[] ch = strName.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			if (isChinese(c)) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 方法描述:判断是否为数字 <br/>
	 * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
	 * Date:2014-11-19 下午4:59:02<br/>
	 * Version:1.0 <br/>
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str){ 
		return isPlusNumeric(str)||isMinusNumeric(str);    
	} 
	
	static Pattern plusNumericPattern = Pattern.compile("^\\d+$|^\\d+\\.\\d+$"); 
	public static boolean isPlusNumeric(String str){ 
		if (isEmpty(str)) {
			return false;
		}
		
		return plusNumericPattern.matcher(str).matches();    
	} 
	
	static Pattern minusNumeric = Pattern.compile("-\\d+$|^-\\d+\\.\\d+$"); 
	public static boolean isMinusNumeric(String str){ 
		if (isEmpty(str)) {
			return false;
		}
		
		return minusNumeric.matcher(str).matches();    
	}
		
	/**
	 * 方法描述:验证字符串是否为电话号码<br/>
	 * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
	 * Date:2014-12-15 下午1:56:53<br/>
	 * Version:1.0 <br/>
	 * @param str
	 * @return
	 */
	static Pattern mobilePhonePattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,2,5-9]))\\d{8}$"); 
	public static boolean isMobilePhone(String str){ 
		if (isEmpty(str)) {
			return false;
		}
		return mobilePhonePattern.matcher(str).matches();    
	}
	/**
	 * 方法描述:验证字符串是否为电子邮箱<br/>
	 * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
	 * Date:2014-12-15 下午1:56:53<br/>
	 * Version:1.0 <br/>
	 * @param str
	 * @return
	 */
	static Pattern emailPattern = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); 
	public static boolean isEmail(String str){
		if (isEmpty(str)) {
			return false;
		}
		return emailPattern.matcher(str).matches();
	}
	/**
	 * 所属类别:字符在字符串中出现的次数<br/>
	 * Author:<a href="mailto:llnyxxzj@163.com">lilong</a> <br/>
	 * Date: 2015年5月12日 上午11:16:51<br/>
	 * Version:1.0 <br/>
	 * @param str
	 * @param subStr 子字符串
	 * @return
	 */
	public static int subCounter(String str, String subStr) {
		if(isEmpty(str)||isEmpty(subStr)){
			return 0;
		}
		int pos = -2;
		int n = 0;
		while (pos != -1) {
			if (pos == -2) {
				pos = -1;
			}
			pos = str.indexOf(subStr, pos + 1);
			if (pos != -1) {
				n++;
			}
		}
		return n;
	}
	/**
	 * 方法描述:将逗号','全部替换为'","'以及字符串前后添加'"'<br/>
	 * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
	 * Date:2015-6-15 上午10:47:30<br/>
	 * Version:1.0 <br/>
	 * <b>注:主要用于SQL中IN条件组装</b>
	 * @param str
	 * @return
	 * @throws BusinessException 业务异常消息,抓取message即可得到相关异常信息
	 */
	public static String replaceAll(String str){
		if(isEmpty(str)){
			return null;
		}
		str=str.replaceAll("\"", "").replaceAll("'", "");
		return "\""+str.replaceAll(",", "\",\"")+"\"";
	}
	
	/**
	 * 方法描述:将逗号','全部替换为'","'以及字符串前后添加'"'<br/>
	 * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
	 * Date:2015-6-15 上午10:47:30<br/>
	 * Version:1.0 <br/>
	 * <b>注:主要用于SQL中IN条件组装</b>
	 * @param str
	 * @return
	 * @throws BusinessException 业务异常消息,抓取message即可得到相关异常信息
	 */
	public static String replaceAll(String regex,String str){
		if(isEmpty(str)||isEmpty(regex)){
			return null;
		}
		str=str.replaceAll("\"", "").replaceAll("'", "");
		return "\""+str.replaceAll(regex, "\",\"")+"\"";
	}
	
	public static String safeString(String str) {
		return str != null ? str : "";
	}
	
	/**
	 * 对字符串中的特殊字符进行unicode编码
	 * @param s 带特殊字符的字符串
	 * @return unicode编码后的字符串
	 */
	public static String encode(String s) {
		if(s == null){
			return null;
		}
        StringBuilder sb = new StringBuilder(s.length() * 3);
        for (char c : s.toCharArray()) {
            if (c < 256) {
                sb.append(c);
            } else {
                sb.append("\\u");
                sb.append(Character.forDigit((c >>> 12) & 0xf, 16));
                sb.append(Character.forDigit((c >>> 8) & 0xf, 16));
                sb.append(Character.forDigit((c >>> 4) & 0xf, 16));
                sb.append(Character.forDigit((c) & 0xf, 16));
            }
        }
        return sb.toString();
    }

	/**
	 * 对unicode编码的字符串进行解码
	 * @param s unicode编码的字符串
	 * @return 解码后的字符串
	 */
    public static String decode(String s) {
    	if(s == null){
    		return null;
    	}
        Matcher m = REUNICODE.matcher(s);
        StringBuffer sb = new StringBuffer(s.length());
        while (m.find()) {
            m.appendReplacement(sb,
                    Character.toString((char) Integer.parseInt(m.group(1), 16)));
        }
        m.appendTail(sb);
        return sb.toString();
    }
	public static String substring(String str, int start, int end) {
		if(str == null) {
			return null;
		} else {
			if(end < 0) {
				end += str.length();
			}

			if(start < 0) {
				start += str.length();
			}

			if(end > str.length()) {
				end = str.length();
			}

			if(start > end) {
				return "";
			} else {
				if(start < 0) {
					start = 0;
				}

				if(end < 0) {
					end = 0;
				}

				return str.substring(start, end);
			}
		}
	}

	/**
	 * 生成编码 -> 随机码可以根据长度生成
	 * @param length
	 * @return
	 */
	public static String getComplexRandomString(int length) {
		String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(62);
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}
	
	
	/**
     * 处理特殊字符串
     * @param str
     * @return
     */
    public static String escapeSpecialChar(String str) {
        String[] stringArr = str.split("");
        String newStr = "";
        for(String s : stringArr){
            if("%".equals(s)) {
                s = s.replace("%","\\%");
            }
            if("\\".equals(s)) {
                s = s.replace("\\","\\\\\\\\");
            }
            if("_".equals(s)) {
                s = s.replace("_","\\_");
            }
            if("\'".equals(s)) {
                s = s.replace("\'","\\'");
            }
            if(".".equals(s)) {
                s = s.replace(".","\\.");
            }
            if("*".equals(s)) {
                s = s.replace("*","\\*");
            }
            newStr += s;
        }
        return newStr;
    }
    
	/**
	* @Title: isNull 
	* @Description: (判断字符串是否是空字符串) 
	* @param str
	* @return boolean    返回类型 true 是空字符串和null，反之返回false
	* @throws
	 */
	public static boolean isBlank(String str) {
		return str == null?true:"".equals(str.trim());
	}
	
	/**
	* @Title: isNotBank 
	* @Description: (判断字符串是否不是空字符串) 
	* @param str
	* @return boolean    返回类型  true 不是空字符串和null，反之返回false
	* @throws
	 */
	public static boolean isNotBlank(String str) {
		return !isBlank(str);
	}
	
	public static String valueOf(Object obj) {
        return (obj == null) ? null : obj.toString();
    }

	public static void main(String[] args) {
	}
}
